//
//  RVTranslateViewController.m
//  RememberVocabularies
//
//  Created by 伍银荣 on 2017/10/14.
//  Copyright © 2017年 WYR. All rights reserved.
//

#import "RVTranslateViewController.h"
#import "FanYiSDK.h"



@interface RVTranslateViewController ()<UITextViewDelegate,UIPickerViewDelegate,UIPickerViewDataSource>

//开始输入文本
@property (strong, nonatomic) IBOutlet UITextView *beginTextView;

@property (strong, nonatomic) IBOutlet UILabel *showLabel;

@property (nonatomic, strong) UILabel *label;

//用于辅助pickerView的view视图
@property (strong, nonatomic) IBOutlet UIView *helpView;
//确定按钮
@property (strong, nonatomic) IBOutlet UIButton *confirmBtn;
//pickerView按钮
@property (strong, nonatomic) IBOutlet UIPickerView *pickerView;
//自动检测语言按钮
@property (strong, nonatomic) IBOutlet UIButton *changeLanguageBtn;
//翻译按钮
@property (strong, nonatomic) IBOutlet UIButton *translationBtn;
//清空数据按钮
@property (strong, nonatomic) IBOutlet UIButton *wipeDataBtn;
//显示翻译后的label
@property (strong, nonatomic) IBOutlet UILabel *showTranslationLabel;

//创建一个存放自动检测语言的数组
@property (nonatomic,strong) NSArray *languageArr;

@property (nonatomic,assign) NSInteger index;

@property (nonatomic, strong) YDTranslateRequest *translateRequest;
@property (nonatomic, strong) YDTranslateParameters *parameters;
@end

@implementation RVTranslateViewController

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [_helpView setHidden:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _languageArr = @[@{@"自动检测语言":@{@"from":@(YDLanguageTypeAuto),@"to":@(YDLanguageTypeAuto)}},
    @{@"中文->英文":@{@"from":@(YDLanguageTypeChinese),@"to":@(YDLanguageTypeEnglish)}},
    @{@"英文->中文":@{@"from":@(YDLanguageTypeEnglish),@"to":@(YDLanguageTypeChinese)}},
    @{@"中文->日文":@{@"from":@(YDLanguageTypeChinese),@"to":@(YDLanguageTypeJapanese)}},
    @{@"日文->中文":@{@"from":@(YDLanguageTypeJapanese),@"to":@(YDLanguageTypeChinese)}},
    @{@"中文->韩语":@{@"from":@(YDLanguageTypeChinese),@"to":@(YDLanguageTypeKorean)}},
    @{@"韩语->中文":@{@"from":@(YDLanguageTypeKorean),@"to":@(YDLanguageTypeChinese)}},
    @{@"中文->法语":@{@"from":@(YDLanguageTypeChinese),@"to":@(YDLanguageTypeFrench)}},
    @{@"法语->中文":@{@"from":@(YDLanguageTypeFrench),@"to":@(YDLanguageTypeChinese)}}];
    
    
    self.navigationItem.title = @"翻译";
    
    self.beginTextView.delegate = self;
    _label = [[UILabel alloc]initWithFrame:CGRectMake(10, 88, 200, 40)];
    _label.textColor = [UIColor lightGrayColor];
    _label.text = @"在此输入要翻译的文本";
    [self.view addSubview:_label];
    
    [self setupUI];
    
    [self setTranslationType];
}

-(void)setTranslationType
{
    //appkey
    YDTranslateInstance *yd = [YDTranslateInstance sharedInstance];
    yd.appKey = @"0b64d5f35bccc188";
    
    //request
    self.translateRequest = [YDTranslateRequest request];
    //设置查询所需要的附加信息
    self.parameters = [YDTranslateParameters targeting];
    self.parameters.source = @"youdaosw";
    self.parameters.from = YDLanguageTypeAuto;
    self.parameters.to = YDLanguageTypeAuto;
    
    self.translateRequest.translateParameters = self.parameters;

}

-(void)setupUI
{
    [self.showTranslationLabel sizeToFit];
    
    //翻译按钮的圆角与边框设置
    _translationBtn.layer.cornerRadius = 4;
    _translationBtn.layer.masksToBounds = YES;
    //清空数据按钮的圆角与边框设置
    _wipeDataBtn.layer.borderWidth = 0.8;
    _wipeDataBtn.layer.borderColor = [UIColor lightGrayColor].CGColor;
    _wipeDataBtn.layer.cornerRadius = 4;
    _wipeDataBtn.layer .masksToBounds = YES;
    
    
    _pickerView.delegate = self;
    _pickerView.dataSource = self;
    [_pickerView selectRow:0 inComponent:0 animated:YES];
    
    //翻译按钮的圆角与边框设置
    _helpView.layer.cornerRadius = 20;
    _helpView.layer.masksToBounds = YES;

    
    
}

//返回一列
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return _languageArr.count;
}

//列标题
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return [[[_languageArr objectAtIndex:row] allKeys] firstObject];
}

//每一列的高度
-(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
    return 44;
}

//每一列的点击
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    _index = row;
}





-(void)textViewDidChange:(UITextView *)textView
{
    if (![textView.text isEqualToString:@""]) {
        _label.hidden = YES;
    } else {
        _label.hidden = NO;
    }
}
//自动检测语言按钮的点击事件
- (IBAction)changeLanguageBtn:(id)sender {
    [self.helpView setHidden:NO];
}

//翻译按钮的点击事件
- (IBAction)TranslationBtn:(id)sender {
    //执行查词过程
    [self.translateRequest lookup:_beginTextView.text WithCompletionHandler:^(YDTranslateRequest *request, YDTranslate *translte, NSError *error) {
        if (error) {
            NSLog(@"%@",error.description);
        } else {
            NSLog(@"%@",translte.translation);
            
            _showTranslationLabel.text = [translte.translation firstObject];
        }
    }];
}
//清空数据按钮的点击事件
- (IBAction)WipeDataBtn:(id)sender {
    _beginTextView.text = @"";
    _showTranslationLabel.text = @"";
    [self textViewDidChange:_beginTextView];
}

//确定按钮的点击事件
- (IBAction)ConfirmBtn:(id)sender {
     NSString *tempKey = [[[_languageArr objectAtIndex:_index] allKeys] firstObject];
    NSLog(@"%@:*****tempKey",tempKey);
    [_changeLanguageBtn setTitle:tempKey forState:UIControlStateNormal];
    NSDictionary *languageType = [[_languageArr objectAtIndex:_index] valueForKey:tempKey];
    NSLog(@"%@:-----languageType",languageType);
    self.parameters.from = [languageType[@"from"] integerValue];
    self.parameters.to = [languageType[@"to"] integerValue];
    [_helpView setHidden:YES];
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [_helpView setHidden:YES];
}








@end
